package com.car.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.car.dto.SysUserDto;
import com.car.entity.*;
import com.car.result.Result;
import com.car.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    public Result<?> login(@RequestBody SysUser sysUser) {
        return userService.userLogin(sysUser);
    }


    @PostMapping("/register")
    public Result<?> register(@RequestBody SysUser sysUser) {
        return userService.userRegister(sysUser);
    }

    @SaCheckRole("admin")
    @PostMapping("/updateUserRole")
    public Result<?> updateUserRole(@RequestBody SysUser sysUser) {
        return userService.updateById(sysUser) ? Result.ok("分配成功") : Result.fail("分配失败");
    }

    @SaCheckRole("admin")
    @GetMapping("/deleteUser/{userid}")
    public Result<?> deleteUser(@PathVariable Integer userid) {
        return userService.removeById(userid) ? Result.ok("删除成功") : Result.fail("删除失败");
    }

    @PostMapping("/updatePassword")
    public Result<?> updatePassword(@RequestBody SysUserDto sysUserDTO) {
        return userService.updatePassword(sysUserDTO);
    }

    @GetMapping("/list")
    public Result<List<SysUser>> list() {
        return Result.ok(userService.list());
    }


}
